﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Tesis.Areas.Modelos.Models;
using Tesis.Models;
using Telerik.Web.Mvc;

namespace Tesis.Areas.Modelos.Controllers
{ 
    public class ModeloController : Controller
    {
        private DBContext db = new DBContext();

        // GET: /Modelos/Modelo/
        public ViewResult Index()
        {
            return View(db.Modelo.ToList());
        }

       
        // GET: /Modelos/Modelo/Edit/5
        [HttpGet]
        public ActionResult Edit(int id)
        {
            Modelo modelo = db.Modelo.Find(id);
            return View(modelo);
        }

        // POST: /Modelos/Modelo/Edit/5
        [HttpPost]
        public ActionResult Edit(Binomial modelo)
        {
            if (ModelState.IsValid)
            {
                db.Entry(modelo).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(modelo);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        #region Vistas Parciales

        public ActionResult _Binomial()
        {
            return PartialView(((Modelo)Session["modelo"]).Binomial);
        }
        public ActionResult _Geometrica()
        {
            return PartialView(((Modelo)Session["modelo"]).Geometrica);
        }
        public ActionResult _Hipergeometrica()
        {
            return PartialView(((Modelo)Session["modelo"]).Hipergeometrica);
        }
        public ActionResult _Pascal()
        {
            return PartialView(((Modelo)Session["modelo"]).Pascal);
        }
        public ActionResult _Poisson()
        {
            return PartialView(((Modelo)Session["modelo"]).Poisson);
        }
        public ActionResult _Uniforme()
        {
            return PartialView(((Modelo)Session["modelo"]).Uniforme);
        }
        public ActionResult _Normal()
        {
            return PartialView(((Modelo)Session["modelo"]).Normal);
        }

        #endregion

        #region Edit Models

        // GET: /Modelos/Modelo/Details/5
        [AcceptVerbs(HttpVerbs.Get)]
        public ViewResult Details(int id)
        {
            Modelo modelo = db.Modelo.Find(id);
            if (id == 1) modelo.Binomial = new Binomial(10, 0.2, 8);
            if (id == 2) modelo.Geometrica = new Geometrica(10, 0.2);
            if (id == 3) modelo.Hipergeometrica = new Hipergeometrica(20,4,15);
            if (id == 4) modelo.Pascal = new Pascal(7,0.5, 20);
            if (id == 5) modelo.Poisson = new Poisson(20,5);
            if (id == 6) modelo.Uniforme = new Uniforme(0.05,0,30);
            if (id == 7) modelo.Normal = new Normal(0.05, 2, 5);
            Session["modelo"] = modelo;
            return View("Details", modelo);
        }

        // Post: /Modelos/Modelo/Details/5
        [HttpPost]
        public ViewResult DetailsBinomial(FormCollection values)
        {
            try
            {
                Binomial binomial;

                if (Convert.ToInt16(values["N"]) < Convert.ToInt16(values["k"]))
                {
                    return View("Details", (Modelo)Session["modelo"]);
                }
                else
                {
                    if (Convert.ToInt16(values["N"]) > 20)
                    {
                        binomial = new Binomial(20, Convert.ToDouble(values["P"]), Convert.ToInt16(values["K"]));
                    }
                    else
                    {
                        binomial = new Binomial(Convert.ToInt16(values["N"]), Convert.ToDouble(values["P"]), Convert.ToInt16(values["K"]));
                    }
                }

                Modelo modelo = new Modelo();
                modelo = (Modelo)Session["modelo"];
                modelo.Binomial = binomial;
                Session["modelo"] = modelo;
                return View("Details", modelo);
            }
            catch
            {
                return View("Details", (Modelo)Session["modelo"]);
            }
        }
        [HttpPost]
        public ViewResult DetailsGeometrica(FormCollection values)
        {
            try
            {
                Geometrica geometrica;
                //debo validar X
                if (Convert.ToInt16(values["N"]) < Convert.ToInt16(values["k"]))
                {
                    return View("Details", (Modelo)Session["modelo"]);
                }
                else
                {
                    if (Convert.ToInt16(values["X"]) > 100)
                    {
                        geometrica = new Geometrica(100, Convert.ToDouble(values["P"]));
                    }
                    else
                    {
                        geometrica = new Geometrica(Convert.ToInt16(values["X"]), Convert.ToDouble(values["P"]));
                    }
                }

                Modelo modelo = new Modelo();
                modelo = (Modelo)Session["modelo"];
                modelo.Geometrica = geometrica;
                Session["modelo"] = modelo;
                return View("Details", modelo);
            }
            catch
            {
                return View("Details", (Modelo)Session["modelo"]);
            }
        }
        [HttpPost]
        public ViewResult DetailsHipergeometrica(FormCollection values)
        {
            try
            {
                Hipergeometrica hipergeometrica;
                //Debo validad N, n, r
                if (Convert.ToInt16(values["N"]) < Convert.ToInt16(values["NN"]))
                {
                    return View("Details", (Modelo)Session["modelo"]);
                }
                else
                {
                    if (Convert.ToInt16(values["N"]) > 100)
                    {
                        hipergeometrica = new Hipergeometrica(100, Convert.ToInt16(values["NN"]), Convert.ToInt16(values["r"]));
                    }
                    else
                    {
                        hipergeometrica = new Hipergeometrica(Convert.ToInt16(values["N"]),Convert.ToInt16(values["NN"]),Convert.ToInt16(values["r"]));
                    }
                }

                Modelo modelo = new Modelo();
                modelo = (Modelo)Session["modelo"];
                modelo.Hipergeometrica = hipergeometrica;
                Session["modelo"] = modelo;
                return View("Details", modelo);
            }
            catch
            {
                return View("Details", (Modelo)Session["modelo"]);
            }
        }
        [HttpPost]
        public ViewResult DetailsPascal(FormCollection values)
        {
            try
            {
                Pascal pascal;
                //Debo validad N, n, r
                if (Convert.ToInt16(values["K"]) < Convert.ToInt16(values["R"]))
                {
                    return View("Details", (Modelo)Session["modelo"]);
                }
                else
                {
                    if (Convert.ToInt16(values["R"]) > 20)
                    {
                        pascal = new Pascal(20, Convert.ToDouble(values["P"]), Convert.ToInt16(values["k"]));
                    }
                    else
                    {
                        pascal = new Pascal(Convert.ToInt16(values["R"]), Convert.ToDouble(values["P"]), Convert.ToInt16(values["k"]));
                    }
                }

                Modelo modelo = new Modelo();
                modelo = (Modelo)Session["modelo"];
                modelo.Pascal = pascal;
                Session["modelo"] = modelo;
                return View("Details", modelo);
            }
            catch
            {
                return View("Details", (Modelo)Session["modelo"]);
            }
        }
        [HttpPost]
        public ViewResult DetailsPoisson(FormCollection values)
        {
            try
            {
                Poisson poisson;
                //Debo validad N, n, r
                if (0 >= Convert.ToInt16(values["L"]))
                {
                    return View("Details", (Modelo)Session["modelo"]);
                }
                else
                {
                    if (Convert.ToInt16(values["X"]) > 20)
                    {
                        poisson = new Poisson(20, Convert.ToDouble(values["L"]));
                    }
                    else
                    {
                        poisson = new Poisson(Convert.ToInt16(values["X"]), Convert.ToDouble(values["L"]));
                    }
                }

                Modelo modelo = new Modelo();
                modelo = (Modelo)Session["modelo"];
                modelo.Poisson = poisson;
                Session["modelo"] = modelo;
                return View("Details", modelo);
            }
            catch
            {
                return View("Details", (Modelo)Session["modelo"]);
            }
        }
        [HttpPost]
        public ViewResult DetailsUniforme(FormCollection values)
        {
            try
            {
                Uniforme uniforme;
                //Debo validad N, n, r
                if (Convert.ToDouble(values["a"]) >= Convert.ToDouble(values["b"]))
                {
                    return View("Details", (Modelo)Session["modelo"]);
                }
                else
                {
                    uniforme = new Uniforme(Convert.ToDouble(values["K"]),Convert.ToDouble(values["a"]), Convert.ToDouble(values["b"]));
                }

                Modelo modelo = new Modelo();
                modelo = (Modelo)Session["modelo"];
                modelo.Uniforme=uniforme;
                Session["modelo"] = modelo;
                return View("Details", modelo);
            }
            catch
            {
                return View("Details", (Modelo)Session["modelo"]);
            }
        }
        [HttpPost]
        public ViewResult DetailsNormal(FormCollection values)
        {
            try
            {
                Normal normal;
                normal = new Normal(Convert.ToDouble(values["K"]), Convert.ToDouble(values["u"]), Convert.ToDouble(values["o"]));

                Modelo modelo = new Modelo();
                modelo = (Modelo)Session["modelo"];
                modelo.Normal = normal;
                Session["modelo"] = modelo;
                return View("Details", modelo);
            }
            catch
            {
                return View("Details", (Modelo)Session["modelo"]);
            }
        }
        #endregion

    }
}